<template>
  <div class="progress-bar" ref="progressBar" @click="progressClick">
    <div class="bar-inner">
      <div class="progress" ref="progress"></div>
      <div
        class="progress-btn-wrapper"
        ref="progressBtn"
        @touchstart.prevent="progressTouchStart"
        @touchmove.prevent="progressTouchMove"
        @touchend.prevent="progressTouchEnd"
      >
        <div class="progress-btn"></div>
      </div>
    </div>
  </div>
</template>
<script>
import { prefixStyle } from "@/utils/dom";
// 按钮宽度：
const ProgressBtnWidth = 16;
const transform = prefixStyle("transform");
export default {
  props: {
    percent: {
      type: Number,
      default: 0
    }
  },
  watch: {
    percent(newPercent) {
      if (newPercent >= 0 && !this.touch.init) {
        const barWidth = this.$refs.progressBar.clientWidth - ProgressBtnWidth;
        const offsetWidth = newPercent * barWidth;
        this._offset(offsetWidth);
      }
    }
  },
  created() {
    // 共享touchstart move end的数据
    this.touch = {};
  },
  methods: {
    progressTouchStart(e) {
      // 记录初始化:
      this.touch.init = true;
      // 记录当前手指的横向坐标
      this.touch.startX = e.touches[0].pageX;
      // 记录下当前button所在的偏移位置：
      this.touch.left = this.$refs.progress.clientWidth;
    },
    progressTouchMove(e) {
      // 如果没有经过点击就移动就直接抛出
      if (!this.touch.init) {
        return;
      }
      // 在纵向的偏移量：
      const deltaX = e.touches[0].pageX - this.touch.startX;
      // 算出移动了多少距离(已经移动的位置加上button的偏移位置)，而且是要小于整个进度条宽度的
      const offsetWidth = Math.min(
        this.$refs.progressBar.clientWidth - ProgressBtnWidth,
        Math.max(0, this.touch.left + deltaX)
      );
      this._offset(offsetWidth);
    },
    progressTouchEnd() {
      this.touch.init = false;
      this._triggerPercent();
    },
    progressClick(e) {
      // getBoundingClientRect()方法用来获取页面中某个元素的左、上、右、下分别相对浏览器视窗的位置，返回的是一个矩形对象，包括四个属性，分别是left 、top、right、bottom。分别表示元素各边与页面上边和左边的距离
      const rect = this.$refs.progressBar.getBoundingClientRect();
      const offsetWidth = e.pageX - rect.left;
      this._offset(offsetWidth);
      // 点击progressBtn的时候e.offsetX获取错误
      // this._offset(e.offsetX);
      this._triggerPercent(offsetWidth);
    },
    _triggerPercent() {
      const barWidth = this.$refs.progressBar.clientWidth - ProgressBtnWidth;
      const percent = this.$refs.progress.clientWidth / barWidth;
      //向父组件派发事件告诉拖动了多少
      this.$emit("percentChange", percent);
    },
    _offset(offsetWidth) {
      this.$refs.progress.style.width = `${offsetWidth}px`;
      // 设置小球偏移
      this.$refs.progressBtn.style[
        transform
      ] = `translate3d(${offsetWidth}px,0,0)`;
    }
  }
};
</script>
<style lang="less" scoped>
.progress-bar {
  height: 30px;
  .bar-inner {
    position: relative;
    top: 13px;
    height: 4px;
    background: rgba(0, 0, 0, 0.3);
  }
  .progress {
    position: absolute;
    height: 100%;
    background: @color-theme;
  }
  .progress-btn-wrapper {
    position: absolute;
    left: -8px;
    top: -13px;
    width: 30px;
    height: 30px;

    .progress-btn {
      position: relative;
      top: 7px;
      left: 7px;
      box-sizing: border-box;
      width: 16px;
      height: 16px;
      border: 3px solid @color-text;
      border-radius: 50%;
      background: @color-theme;
    }
  }
}
</style>